#include "entityeditor.h"
#include "ui_entityeditor.h"

EntityEditor::EntityEditor(QWidget *parent,const QSqlDatabase &db_) :
    QDialog(parent),
    ui(new Ui::EntityEditor)
{
    ui->setupUi(this);

    table=REF_STATEMENT;
    db=db_;
    model=new QStringListModel(this);
    ui->listView->setModel(model);

    ui->labels->addItems(statementHeaders);
    ui->labels->setCurrentIndex(-1);

    updateListView();
}

EntityEditor::~EntityEditor()
{
    delete ui;
}

QVariant EntityEditor::fetchItem(const QString &sql)
{
    QSqlQuery query(db);
    query.exec(sql);
    if(query.next())
        return query.value(0);
    else
        return QVariant();
}

QStringList EntityEditor::fetchItems(const QString &sql)
{
    QStringList items;

    QSqlQuery query(db);
    query.exec(sql);
    while (query.next()) {
        QString item=query.value(0).toString();
        items.append(item);
    }
    return items;
}

void EntityEditor::updateListView()
{
    QString sql=QString("select entry from %1 group by entry").arg(table);
    auto items=fetchItems(sql);
    model->setStringList(items);
}

void EntityEditor::on_listView_doubleClicked(const QModelIndex &index)
{
    if(index.isValid())
    {
        selectEntry=index.data().toString();
        setWindowTitle(QString("实体编辑器 - %1").arg(selectEntry));
        ui->labels->setCurrentIndex(-1);
        ui->detail->clear();
    }
}

void EntityEditor::on_labels_currentTextChanged(const QString &label)
{
    selectLabel=label;
    QString sql=QString("select detail from %1 where entry='%2' AND label='%3'")
            .arg(table)
            .arg(selectEntry)
            .arg(selectLabel);

    auto detail=fetchItem(sql).toString();
    ui->detail->setPlainText(detail);
}

bool EntityEditor::addItem(const QString &detail)
{
    QSqlQuery query(db);
    QString sql=QString("insert into %1 (entry,label,detail) values (?,?,?)")
            .arg(table);
    query.prepare(sql);
    query.addBindValue(selectEntry);
    query.addBindValue(selectLabel);
    query.addBindValue(detail);
    return query.exec();
}

bool EntityEditor::updateItem(const QString &detail, int id)
{
    QSqlQuery query(db);
    QString sql=QString("update %1 set detail='%2' where id=%3")
            .arg(table)
            .arg(detail)
            .arg(id);
    return query.exec(sql);
}

void EntityEditor::on_btn_submit_clicked()
{
    if(selectEntry.isEmpty() || selectLabel.isEmpty())
        return;

    QString detail=ui->detail->toPlainText();

    QString sql=QString("select id from %1 where entry='%2' AND label='%3'")
            .arg(table)
            .arg(selectEntry)
            .arg(selectLabel);
    int id=fetchItem(sql).toInt();
    if(id == 0)
    {
        if(addItem(detail))
            QMessageBox::information(this,"操作已完成","数据已提交");
        else
            QMessageBox::warning(this,"操作失败","无法提交数据");
    }
    else
    {
        if(updateItem(detail,id))
            QMessageBox::information(this,"操作已完成","数据已保存");
        else
            QMessageBox::warning(this,"操作失败","无法保存数据");
    }
}
